<template>
  <el-form
    ref="formRef"
    class="k-search"
    v-bind="$attrs"
    :label-width="labelWidth"
    :inline="inline"
    @keyup.enter="onSearch"
    @submit.prevent="onSearch"
  >
    <slot></slot>

    <div class="k-search-bottom">
      <div class="bottom__content">
        <slot name="bottom"></slot>
      </div>
      <KSearchActions @search="onSearch" @reset="onReset" />
    </div>
  </el-form>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { ElForm } from 'element-plus';
import KSearchActions from './k-search-actions.vue';

defineOptions({
  name: 'KSearch',
  inheritAttrs: false
});

interface Props {
  labelWidth?: string;
  inline?: boolean;
}
withDefaults(defineProps<Props>(), {
  labelWidth: '72px',
  inline: true
});

const formRef = ref();
const emits = defineEmits(['search', 'reset']);
const onSearch = async () => {
  await formRef.value.validate();
  emits('search');
};
const onReset = () => {
  emits('reset');
};
defineExpose({
  formRef
});
</script>

<style lang="scss">
.k-search {
  &.el-form--inline {
    .el-form-item {
      margin-right: 24px;
    }

    // 输入框 && 下拉框
    .el-input, .el-select {
      width: 180px;
    }

    // 日期区间
    .el-date-editor--daterange {
      width: 328px;
    }
  }
}

.k-search-bottom {
  display: flex;

  .bottom__content {
    flex: 1;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}

// :deep(.el-form--inline) {
//   .el-form-item {
//     align-items: center;
//     min-height: 32px;
//     margin-bottom: 0;
//   }
// }
</style>
